package com.etop.bank.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.bank_ocr.R;


/**
 * 矩形扫描框
 */
public class ScanRectView extends View {
	private Paint paintCorner;
	private Paint paintLine;
	private Rect rectTop;
	private Rect rectBottom;
	private Rect rectLeft;
	private Rect rectRight;

	private int lineWidth;
	private int lineLength;

	private int titleHeight;
	private Paint mTextPaint;
	private boolean isVertical = false;

	private boolean isShowBankNumber = true;
	private String mBankNumber = "1234  5678  9012  3456";

	public ScanRectView(Context context) {
		this(context, null);
	}

	public ScanRectView(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, -1);
	}

	public ScanRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		titleHeight = (int) getResources().getDimension(R.dimen.camera_top_margin);
		lineLength = (int) getResources().getDimension(R.dimen.camera_rect_line_length);
		lineWidth = (int) getResources().getDimension(R.dimen.camera_rect_lint_width);
		paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintLine.setColor(Color.parseColor("#59D4D1"));
		paintLine.setStyle(Paint.Style.STROKE);//设置空心
		paintLine.setStrokeWidth(lineWidth);

		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mTextPaint.setColor(Color.parseColor("#59D4D1"));
		mTextPaint.setStrokeWidth(lineWidth);


		paintCorner = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintCorner.setColor(Color.parseColor("#66000000"));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int width = getWidth();
		int height = getHeight();
		int left, top, right, bottom;
		int rectHeight;

		if (isVertical) {
			//银行卡比例：0.6307;
			left = (int) (width * 0.02);
			right = width - left;
			rectHeight = (int) ((right - left) / 1.6);
			top = (height - rectHeight) / 2;
			bottom = rectHeight + top;
		} else {
			//银行卡比例：0.6307;
			left = (int) (width * 0.13);
			right = width - left;
			rectHeight = (int) ((right - left) * 1.52);
			top = (height - rectHeight) / 2;
			bottom = rectHeight + top;
		}

		//绘制四个半透明矩形
		rectTop = new Rect(0, 0, width, top);
		canvas.drawRect(rectTop, paintCorner);

		rectBottom = new Rect(0, bottom, width, height);
		canvas.drawRect(rectBottom, paintCorner);

		rectLeft = new Rect(0, top, left, bottom);
		canvas.drawRect(rectLeft, paintCorner);
		rectRight = new Rect(right, top, width, bottom);
		canvas.drawRect(rectRight, paintCorner);


		paintLine.setColor(Color.parseColor("#59D4D1"));
		//left + top
		canvas.drawLine(left, top, left + lineLength, top, paintLine);
		canvas.drawLine(left, top, left, top + lineLength, paintLine);

		//right + top
		canvas.drawLine(right, top, right - lineLength, top, paintLine);
		canvas.drawLine(right, top, right, top + lineLength, paintLine);

		//left + bottom
		canvas.drawLine(left, bottom, left + lineLength, bottom, paintLine);
		canvas.drawLine(left, bottom, left, bottom - lineLength, paintLine);

		//right + bottom
		canvas.drawLine(right, bottom, right - lineLength, bottom, paintLine);
		canvas.drawLine(right, bottom, right, bottom - lineLength, paintLine);

		// 显示银行卡号
		if (isShowBankNumber) {
			int textSize = width / 12;
			if (isVertical)
				textSize = width / 14;
			mTextPaint.setTextSize(textSize);
			Rect rect = new Rect();
			mTextPaint.getTextBounds(mBankNumber, 0, mBankNumber.length(), rect);
			int textWidth = rect.width();
			int paddingTop = (int) ((rectHeight - textWidth) / 2) - rect.height() / 2;
			int px;
			if (isVertical) {
				px = (width - textWidth) / 2;
				paddingTop = (int) (width / 2.7);
				canvas.rotate(0, 0, top + paddingTop);
				canvas.drawText(mBankNumber, px, top + paddingTop, mTextPaint);
			} else {
				px = (int) (width / 2.5);
				canvas.rotate(90, px, top + paddingTop);
				canvas.drawText(mBankNumber, px + 20, top + paddingTop, mTextPaint);
			}
		}
		postInvalidateDelayed(50);//刷新界面
	}

	public void setBankNumber(boolean isShowBankNumber) {
		this.isShowBankNumber = isShowBankNumber;
	}

	public void setIsVertical(boolean isVertical) {
		this.isVertical = isVertical;
	}
}
